package com.kuiandjie.web;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.kuiandjie.entity.Contact;
import com.kuiandjie.entity.Employee;
import com.kuiandjie.entity.Log;
import com.kuiandjie.entity.Project;
import com.kuiandjie.service.ConcatService;
import com.kuiandjie.service.LogService;
import com.kuiandjie.util.DateUtil;

public class EditConcateServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		Project curp = (Project) session.getAttribute("curp");
		
		if("GET".equals(request.getMethod())){
			String str = request.getParameter("id");
			if(curp != null && str != null && !"".equals(str)) {
				int id = Integer.parseInt(str);
				Contact c = new ConcatService().findContactByIdAndProId(id, curp.getId());
				
				if(c != null) {
					request.setAttribute("contact", c);
					request.getRequestDispatcher("WEB-INF/views/editcontact.jsp").forward(request, response);
				} else {
					response.sendRedirect("main.jspx");
				}
			} else {
				response.sendRedirect("main.jspx");
			}
		} else {
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			String name = request.getParameter("name");
			String company = request.getParameter("company");
			String moblie = request.getParameter("moblie");
			String tel = request.getParameter("tel");
			String email = request.getParameter("email");
			String address = request.getParameter("address");
			String website = request.getParameter("website");
			String weibo = request.getParameter("weibo");
			String content = request.getParameter("content");
			String id = request.getParameter("id");
			
			if(id.trim() == "" || name.trim() == "" || company.trim() == "" || moblie.trim() == "" || tel.trim() == "" || email.trim() == "") {
				out.print("1002");
			} else {
				Employee employee = (Employee) session.getAttribute("employee");
				int contactId = Integer.parseInt(id);
				Contact c = new Contact();
				c.setId(contactId);
				c.setName(name);
				c.setCompanyname(company);
				c.setMoblie(moblie);
				c.setTel(tel);
				c.setEmail(email);
				c.setAddress(address);
				c.setWebsite(website);
				c.setWeibo(weibo);
				c.setContent(content);
				new ConcatService().editContact(c);
				Log log = new Log();
				log.setDesc("编辑了联系人<a href='contact.jspx'>" + name + "</a>");
				log.setEmployeeId(employee.getId());
				log.setProjectId(curp.getId());
				log.setTime(DateUtil.getNow());
				log.setName(employee.getName());
				log.setPic(employee.getPic());
				
				LogService ls = new LogService();
				ls.inserLog(log);
				
				request.setAttribute("msg", new Gson().toJson(log));
				request.getRequestDispatcher("msgflow.jspx").include(request, response);
				out.print("1001");
			}
			
			out.flush();
			out.close();
		}
	}
}
